package dao;
/*
增添用户信息,即可以对数据库进行增加
如果增加成功，返回true
失败返回false
 */

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.SQLException;

public class addUser{
    //1.获取连接池Datasource对像
    private static DataSource dataSource = JDBCUtils.getDataSource();

    //2.使用DBUtils
    private static QueryRunner qr = new QueryRunner(dataSource);

    public boolean CheckUser(String username) throws SQLException {
        //3.查询操作
        Long count = (Long)qr.query("select count(*) from user where name = ?", new ScalarHandler(), username);
        //判断用户名是否能够注册
        //如果count >= 1说明该用户名已被注册，需要提示用户换一个名字注册
        if(count < 1){
            return true;
        }
        else{
            return false;
        }
    }

    public boolean addUser(String username, String password) throws SQLException {
        System.out.println("connect database and do the addUser()...");

        int update = qr.update("insert into user values (?,?)", username, password);

        if(update == 1){
            return true;
        }else{
        System.out.println(update);
        return false;}
    }


}
